package normal.test

import auth.sys.Menu
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.TypeReference
import com.alibaba.fastjson.parser.Feature
import fundation.gsonPrint
import fundation.gsonPrintWithoutNull
import org.junit.Test
import other.PClass.a

/**
 * @author futao
 * Created on 2017/11/29 - 10:34.
 */
class TestClass001 {
    /**
     * 每次菜单更新都去 更新每个角色的菜单信息
     */
    @Test
    fun testCase001() {
        val menu = "{\"mall\":{\"menu\":{\"route\":\"mall\",\"name\":\"mall\",\"moduleId\":\"menu/menu-mall\",\"nav\":true,\"role\":[3],\"title\":\"商城\"},\"menuMap\":{\"operation\":{\"id\":\"operation\",\"title\":\"运营管理\",\"items\":[{\"id\":\"mall-operation-summary\",\"href\":\"#mall-operation-summary\",\"moduleId\":\"mall/operation/summary\",\"title\":\"商城概览\",\"icon\":\"fa-paper-plane\"},{\"id\":\"mall-operation-setting\",\"href\":\"#mall-operation-setting\",\"moduleId\":\"mall/operation/setting\",\"title\":\"商城设置\",\"icon\":\"fa-cog\"},{\"id\":\"mall-operation-subjects\",\"href\":\"#mall-operation-subjects\",\"moduleId\":\"mall/operation/subjects\",\"icon\":\"fa-calendar-o\",\"title\":\"活动专题\"},{\"id\":\"mall-operation-searchTerms\",\"href\":\"#mall-operation-searchTerms\",\"moduleId\":\"mall/operation/searchTerms\",\"icon\":\" fa-search\",\"title\":\"搜索关键词\"}]},\"goods\":{\"id\":\"goods\",\"title\":\"商品管理\",\"icon\":\"fa-building\",\"items\":[{\"id\":\"mall-goods-detail\",\"href\":\"#mall-goods-detail\",\"moduleId\":\"mall/goods/detail\",\"title\":\"商品发布\",\"style\":\"btn\"},{\"id\":\"mall-goods-sales\",\"href\":\"#mall-goods-sales\",\"moduleId\":\"mall/goods/sales\",\"title\":\"出售中的商品\",\"icon\":\"fa-black-tie\"},{\"id\":\"mall-goods-outsales\",\"href\":\"#mall-goods-outsales\",\"moduleId\":\"mall/goods/outsales\",\"title\":\"仓库中的商品\",\"icon\":\"fa-cubes\",\"line\":true},{\"id\":\"mall-goods-group\",\"href\":\"#mall-goods-group\",\"moduleId\":\"mall/goods/group/groups\",\"title\":\"分组管理\",\"icon\":\"fa-clone\"},{\"id\":\"mall-goods-prop\",\"href\":\"#mall-goods-prop\",\"moduleId\":\"mall/goods/prop/props\",\"title\":\"属性规格\",\"icon\":\"fa-bars\"},{\"id\":\"mall-goods-freight\",\"href\":\"#mall-goods-freight\",\"moduleId\":\"mall/goods/freight/templates\",\"title\":\"运费模板\",\"icon\":\"fa-cube\"},{\"id\":\"mall-goods-tags\",\"href\":\"#mall-goods-tags\",\"moduleId\":\"mall/goods/tag/tags\",\"title\":\"标签管理\",\"icon\":\"fa-tags\"}]},\"shop\":{\"id\":\"shop\",\"title\":\"订单管理\",\"icon\":\"fa-cart-plus\",\"items\":[{\"id\":\"mall-shop-ordersList\",\"href\":\"#mall-shop-ordersList\",\"moduleId\":\"mall/shop/order/ordersList\",\"icon\":\"fa-outdent\",\"role\":[3],\"title\":\"订单列表\"},{\"id\":\"mall-shop-toSendGoods\",\"href\":\"#mall-shop-toSendGoods\",\"moduleId\":\"mall/shop/order/toSendGoods\",\"icon\":\"fa-cubes\",\"hide\":true,\"title\":\"待发货\"},{\"id\":\"mall-shop-reviewManage\",\"href\":\"#mall-shop-reviewManage\",\"moduleId\":\"mall/shop/review/reviewManage\",\"icon\":\"fa-commenting\",\"role\":[3],\"title\":\"评价管理\"},{\"id\":\"mall-shop-order-export\",\"href\":\"#mall-shop-order-export\",\"moduleId\":\"mall/shop/order/export\",\"icon\":\"fa-commenting\",\"role\":[3],\"title\":\"导出\"}]},\"decorate\":{\"id\":\"decorate\",\"title\":\"页面装修\",\"hide\":true,\"icon\":\"fa-th\",\"items\":[{\"id\":\"mall-decorate-index\",\"href\":\"#mall-decorate-index\",\"moduleId\":\"mall/decorate/mall-index\",\"title\":\"首页装修\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-banner\",\"href\":\"#mall-decorate-banner\",\"moduleId\":\"mall/decorate/banner\",\"title\":\"广告位装修\",\"hide\":true,\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-product-list\",\"href\":\"#mall-decorate-product-list\",\"moduleId\":\"mall/decorate/product-list\",\"title\":\"商品列表\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-product-detail\",\"href\":\"#mall-decorate-product-detail\",\"moduleId\":\"mall/decorate/product-detail\",\"title\":\"商品详情\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-user-center\",\"href\":\"#mall-decorate-user-center\",\"moduleId\":\"mall/decorate/user-center\",\"title\":\"用户中心\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-footer-index\",\"href\":\"#mall-decorate-footer-index\",\"moduleId\":\"mall/decorate/footer-index\",\"title\":\"底部装修\",\"icon\":\"fa-calendar-minus-o\"}]},\"manage-resources\":{\"id\":\"manage-resources\",\"href\":\"#manage-resources\",\"moduleId\":\"manage/resources\",\"title\":\"资源空间\",\"icon\":\"fa-image\"}}},\"marketing\":{\"menu\":{\"route\":\"marketing\",\"name\":\"marketing\",\"moduleId\":\"menu/menu-marketing\",\"nav\":true,\"role\":[3],\"title\":\"营销\"},\"menuMap\":{\"discount\":{\"id\":\"discount\",\"title\":\"促销\",\"items\":[{\"id\":\"marketing-discount-discounts\",\"href\":\"#marketing-discount-discounts\",\"moduleId\":\"marketing/discount/discounts\",\"icon\":\"fa-credit-card\",\"title\":\"优惠券\"}]}}},\"manage\":{\"menu\":{\"name\":\"manage\",\"route\":\"manage\",\"moduleId\":\"menu/menu-manage\",\"nav\":true,\"title\":\"系统管理\",\"roleType\":\"1\"},\"menuMap\":{\"shops\":{\"id\":\"manage-shops\",\"href\":\"#manage-shops\",\"moduleId\":\"manage/shops\",\"icon\":\"fa-address-card-o\",\"title\":\"商家管理\"},\"category\":{\"id\":\"manage-category\",\"href\":\"#manage-category\",\"moduleId\":\"manage/category\",\"icon\":\"fa-bars\",\"title\":\"类别管理\"},\"roles-setting\":{\"id\":\"roles-setting\",\"href\":\"#manage-roles-setting\",\"moduleId\":\"manage/roles\",\"icon\":\"fa-address-card-o\",\"title\":\"角色管理\"},\"menus-setting\":{\"id\":\"menus-setting\",\"href\":\"#manage-menus-setting\",\"moduleId\":\"manage/menus-setting\",\"icon\":\"fa-address-card-o\",\"title\":\"菜单管理\"},\"stuff\":{\"id\":\"manage-stuff\",\"href\":\"#manage-stuff\",\"moduleId\":\"manage/stuff\",\"icon\":\"fa-calendar-minus-o\",\"title\":\"员工列表\"},\"caches\":{\"id\":\"manage-caches\",\"href\":\"#manage-caches\",\"moduleId\":\"manage/caches\",\"icon\":\"fa-database\",\"title\":\"缓存管理\"}}},\"member\":{\"menu\":{\"name\":\"member\",\"route\":\"member\",\"moduleId\":\"menu/menu-member\",\"nav\":true,\"title\":\"会员\",\"roleType\":\"1\"},\"menuMap\":{\"member\":{\"id\":\"member\",\"title\":\"会员管理\",\"icon\":\"fa-users\",\"items\":[{\"id\":\"integral-management\",\"href\":\"integral-management\",\"moduleId\":\"member/integral-management\",\"title\":\"积分设置\",\"icon\":\"fa-star-o\"},{\"id\":\"grade-management\",\"href\":\"grade-management\",\"moduleId\":\"member/grade-management\",\"title\":\"等级设置\",\"icon\":\"fa-sliders\"},{\"id\":\"member-management\",\"href\":\"member-management\",\"moduleId\":\"member/member-manage\",\"title\":\"会员管理\",\"icon\":\"fa-user-circle\"}]}}},\"sales\":{\"menu\":{\"route\":\"sales\",\"name\":\"sales\",\"moduleId\":\"menu/menu-sales\",\"nav\":true,\"role\":[4,5],\"title\":\"销售\"},\"menuMap\":{\"goods-pick\":{\"id\":\"goods-pick\",\"moduleId\":\"sales-manage/goods/pick\",\"href\":\"#goods-pick\",\"role\":[4],\"title\":\"提货\"},\"refund-good\":{\"id\":\"mall-shop-orderRefund\",\"href\":\"#mall-shop-orderRefund\",\"moduleId\":\"mall/shop/order/ordersRefundList\",\"icon\":\"fa-commenting\",\"title\":\"退货\"}}},\"setting\":{\"menu\":{\"route\":\"setting\",\"name\":\"setting\",\"moduleId\":\"menu/menu-setting\",\"nav\":true,\"title\":\"设置\"},\"menuMap\":{\"changePwd\":{\"id\":\"changePwd\",\"title\":\"修改密码\",\"href\":\"#setting/changePwd\",\"moduleId\":\"setting/changePwd\",\"icon\":\"fa-cog\",\"line\":true},\"logout\":{\"id\":\"logout\",\"title\":\"退出登录\",\"href\":\"#setting/logout\",\"icon\":\"fa-power-off\"}}},\"test\":{\"menu\":{\"route\":\"test\",\"name\":\"test\",\"moduleId\":\"menu/menu-test\",\"nav\":true,\"title\":\"测试\"},\"menuMap\":{\"test\":{\"id\":\"test\",\"moduleId\":\"test/test\",\"href\":\"#test\",\"title\":\"测试\"},\"test-sub\":{\"id\":\"test-two-menu\",\"title\":\"二级菜单\",\"items\":[{\"id\":\"test-chart\",\"href\":\"#test-chart\",\"moduleId\":\"test/chart-test\",\"title\":\"testChart\"},{\"id\":\"test-sub1\",\"href\":\"#test-sub1\",\"moduleId\":\"test/test\",\"title\":\"testSub1\"},{\"id\":\"test-sub2\",\"href\":\"#test-sub2\",\"moduleId\":\"test/test-sub2\",\"title\":\"testSub2\"}]}}}}"


        /**
         * 把整个json转换成map
         * <"mall":"">
         */
        val maps = JSON.parseObject(menu) as Map<String, *>
        /**
         * 第一层 keys
         */
        maps.forEach { it ->
            run {
                /**
                 * <"menu":"">
                 */
                val menuOrMap = JSON.parseObject(it.value.toString()) as Map<String, *>
                menuOrMap.forEach { menuIt ->
                    run {
                        when (menuIt.key) {
                            "menu" -> {
                                val menu = JSON.parseObject(menuIt.value.toString()) as Map<String, *>
                                /**
                                 * "title": "商城"
                                 * "role": [3],
                                 */
                                menu.forEach { it ->
                                    run {
                                        when (it.key) {
                                            "role" -> {
                                                val parseArray = JSON.parseArray(it.value.toString())
                                                println(parseArray)
                                            }
                                            "item" -> {

                                            }
                                            else -> {
                                                val parseObject = JSON.parseObject(it.value.toString()) as Map<*, *>
                                                println(parseObject)
                                            }
                                        }
                                    }
                                }
                            }
                            "menuKey" -> {
                                val menuMap = JSON.parseObject(menuIt.value.toString()) as Map<*, *>
                                println(menuMap.keys)
                                println(menuMap.values)
                            }
                        }
                    }

                }
            }
        }
    }

    @Test
    fun testCase002() {
        val mapOf = mapOf<Int, String>()
        val hashMapOf = hashMapOf<Int, String>()
        hashMapOf[10] = "qqqq"
        hashMapOf[2] = "wwww"
        hashMapOf.map { it ->
            run {
                println(it.value)
                var value = it.value
                value = "222222222222222222"
            }
        }
        gsonPrint(hashMapOf)
    }

    @Test
    fun testCase003() {

        /*json->String*/
        val readAuth = package001.JsonUtils.readJson("D:\\kotlinstudynew\\src\\test\\resource\\auth.json")
        val readMenu = package001.JsonUtils.readJson("D:\\kotlinstudynew\\src\\test\\resource\\menu.json")
        /*所有角色信息*/
        val roles = listOf(1, 2, 3, 4)
        val authObj = fastJsonParseObject(readAuth)
        /*遍历每个角色*/
        roles.forEach { role ->
            run {
                /**
                 * 遍历每一个导航  Nav
                 *
                 * "mall",""
                 */
                fastJsonParseObject(readMenu)?.forEach { itNav ->
                    run {
                        /**
                         * 遍历   menu 和 menuMap
                         *
                         * "menu":""
                         * "menuMap":""
                         *
                         * menu
                         */
                        val menu = fastJsonParseObject(itNav.value)["menu"]
                        run {
                            /**
                             * "route":""
                             * "role":""
                             */

                            val menuContent = menu?.let { JSON.parseObject(menu, Menu::class.java) as Menu }!!
                            /*roleType存在*/
                            if (menuContent.roleType != null) {
                                /**
                                 * 符合当前角色
                                 */
                                if (menuContent.roleType == role) {
                                    /**
                                     * 是否隐藏
                                     */
                                    if ((menuContent.nav)) {
                                        /*判断当前角色是否有该菜单，没有就添加*/
                                        if (!authObj.keys.contains(itNav.key)) {
                                            val nav = LinkedHashMap<String, Menu>()
                                            menuContent.auth = arrayOf(authShow, authView.apply { isVal = false })
                                            menuContent.nav = null
                                            menuContent.route = null
                                            menuContent.moduleId = null
                                            menuContent.roleType = null
                                            nav.set(key = itNav.key, value = menuContent)
                                            gsonPrintWithoutNull(nav)
                                            authObj[itNav.key] = JSON.toJSONString(nav)
                                            println("555555555555555555555")
                                            println(JSON.toJSONString(nav))
                                        }
                                    }
                                }
                            }
                            /*不存在角色限制，所有角色都添加*/
                            else {

                            }

//                            println(authObj.keys)
//                            println(it.key)
                            if (!authObj.keys.contains(itNav.key)) {
                                authObj.put(itNav.key, itNav.value)
//                                println("==========${it.key}")
                            }
//                println("==mall-key==${it.key}")
//                println("==mall-value==${it.value}")
                        }

                        val menuMap = fastJsonParseObject(itNav.value)["menuMap"]!!
                        /**
                         *  menuMap  Content
                         */
                        run {
                            // println("hhhhhhhhhhhhhhhhhh${menuMap}")
                        }
                    }
                }

            }
        }
        println("6666666666666666666666666666666666666666666666${JSON.toJSONString(authObj)}")


    }

    @Test
    fun testCase005() {
        val one = fastJsonParseObject(authJson)
        one.forEach { it ->
            run {
                it.key
                it.value
            }

        }
    }

    @Test
    fun testCase006() {
        val listOf = listOf(1, 2, 3, 4, 5, 6)
        val listOf1 = listOf("a1", "a2", "a3", "a4", "a5", "a6")

        listOf.forEach f1@ { it ->
            kotlin.run {
                listOf1.forEach f2@ { it2 ->
                    kotlin.run {
                        if (it2 == "a3") {
                            return@f1
                        } else {
                            println(it2)
                            println("666")
                        }
                    }
                }
            }
        }
    }

    @Test
    fun testCase007() {
        val linkedHashMap = LinkedHashMap<String, String>()
        linkedHashMap.put("7", "7")
        println(linkedHashMap["66666"])
        linkedHashMap.containsKey("751")
        val s = linkedHashMap["aaa"].toString()
        println(a.toString())
    }

    @Test
    fun testCse008() {
        val mall = "{\"senu\":{\"name\":\"mall\",\"route\":\"mall\",\"moduleId\":\"menu/menu-mall\",\"nav\":true,\"title\":\"商城\",\"roleType\":\"\"},\"menuMap\":{\"operation\":{\"id\":\"operation\",\"title\":\"运营管理\",\"items\":[{\"id\":\"mall-operation-summary\",\"href\":\"#mall-operation-summary\",\"title\":\"商城概览\",\"icon\":\"fa-paper-plane\",\"style\":\"\",\"line\":false,\"hide\":false,\"roleType\":\"1\"},{\"id\":\"mall-operation-setting\",\"href\":\"#mall-operation-setting\",\"moduleId\":\"mall/operation/setting\",\"title\":\"商城设置\",\"icon\":\"fa-cog\"},{\"id\":\"mall-operation-subjects\",\"href\":\"#mall-operation-subjects\",\"moduleId\":\"mall/operation/subjects\",\"icon\":\"fa-calendar-o\",\"title\":\"活动专题\"},{\"id\":\"mall-operation-searchTerms\",\"href\":\"#mall-operation-searchTerms\",\"moduleId\":\"mall/operation/searchTerms\",\"icon\":\" fa-search\",\"title\":\"搜索关键词\"}],\"line\":false,\"href\":\"#operation\",\"hide\":false,\"roleType\":\"1\",\"style\":\"\"},\"goods\":{\"id\":\"goods\",\"title\":\"商品管理\",\"icon\":\"fa-building\",\"items\":[{\"id\":\"mall-goods-detail\",\"href\":\"#mall-goods-detail\",\"moduleId\":\"mall/goods/detail\",\"title\":\"商品发布\",\"style\":\"btn\"},{\"id\":\"mall-goods-sales\",\"href\":\"#mall-goods-sales\",\"moduleId\":\"mall/goods/sales\",\"title\":\"出售中的商品\",\"icon\":\"fa-black-tie\"},{\"id\":\"mall-goods-outsales\",\"href\":\"#mall-goods-outsales\",\"moduleId\":\"mall/goods/outsales\",\"title\":\"仓库中的商品\",\"icon\":\"fa-cubes\",\"line\":true},{\"id\":\"mall-goods-group\",\"href\":\"#mall-goods-group\",\"moduleId\":\"mall/goods/group/groups\",\"title\":\"分组管理\",\"icon\":\"fa-clone\"},{\"id\":\"mall-goods-prop\",\"href\":\"#mall-goods-prop\",\"moduleId\":\"mall/goods/prop/props\",\"title\":\"属性规格\",\"icon\":\"fa-bars\"},{\"id\":\"mall-goods-freight\",\"href\":\"#mall-goods-freight\",\"moduleId\":\"mall/goods/freight/templates\",\"title\":\"运费模板\",\"icon\":\"fa-cube\"},{\"id\":\"mall-goods-tags\",\"href\":\"#mall-goods-tags\",\"moduleId\":\"mall/goods/tag/tags\",\"title\":\"标签管理\",\"icon\":\"fa-tags\"}]},\"shop\":{\"id\":\"shop\",\"title\":\"订单管理\",\"icon\":\"fa-cart-plus\",\"items\":[{\"id\":\"mall-shop-ordersList\",\"href\":\"#mall-shop-ordersList\",\"moduleId\":\"mall/shop/order/ordersList\",\"icon\":\"fa-outdent\",\"role\":[3],\"title\":\"订单列表\"},{\"id\":\"mall-shop-toSendGoods\",\"href\":\"#mall-shop-toSendGoods\",\"moduleId\":\"mall/shop/order/toSendGoods\",\"icon\":\"fa-cubes\",\"hide\":true,\"title\":\"待发货\"},{\"id\":\"mall-shop-reviewManage\",\"href\":\"#mall-shop-reviewManage\",\"moduleId\":\"mall/shop/review/reviewManage\",\"icon\":\"fa-commenting\",\"role\":[3],\"title\":\"评价管理\"},{\"id\":\"mall-shop-order-export\",\"href\":\"#mall-shop-order-export\",\"moduleId\":\"mall/shop/order/export\",\"icon\":\"fa-commenting\",\"role\":[3],\"title\":\"导出\"}]},\"decorate\":{\"id\":\"decorate\",\"title\":\"页面装修\",\"hide\":true,\"icon\":\"fa-th\",\"items\":[{\"id\":\"mall-decorate-index\",\"href\":\"#mall-decorate-index\",\"moduleId\":\"mall/decorate/mall-index\",\"title\":\"首页装修\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-banner\",\"href\":\"#mall-decorate-banner\",\"moduleId\":\"mall/decorate/banner\",\"title\":\"广告位装修\",\"hide\":true,\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-product-list\",\"href\":\"#mall-decorate-product-list\",\"moduleId\":\"mall/decorate/product-list\",\"title\":\"商品列表\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-product-detail\",\"href\":\"#mall-decorate-product-detail\",\"moduleId\":\"mall/decorate/product-detail\",\"title\":\"商品详情\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-user-center\",\"href\":\"#mall-decorate-user-center\",\"moduleId\":\"mall/decorate/user-center\",\"title\":\"用户中心\",\"icon\":\"fa-calendar-minus-o\"},{\"id\":\"mall-decorate-footer-index\",\"href\":\"#mall-decorate-footer-index\",\"moduleId\":\"mall/decorate/footer-index\",\"title\":\"底部装修\",\"icon\":\"fa-calendar-minus-o\"}]},\"manage-resources\":{\"id\":\"manage-resources\",\"href\":\"#manage-resources\",\"moduleId\":\"manage/resources\",\"title\":\"资源空间\",\"icon\":\"fa-image\"}}}"

        val parseObject = JSON.parseObject(mall, object : TypeReference<java.util.LinkedHashMap<String, String>>() {}, Feature.OrderedField)
        val menu = LinkedHashMap<Any, Any>()
        parseObject.forEach { (k, v) ->
            kotlin.run {
                println(k)
                when (k) {
                    "menu" -> {
//                        JSON.parseObject(parseObject["menu"], object : TypeReference<java.util.LinkedHashMap<String, String>>() {})

                    }
                    "menuMap" -> {
                        val menuMap = JSON.parseObject(parseObject["menuMap"], object : TypeReference<java.util.LinkedHashMap<String, String>>() {}, Feature.OrderedField)
                        menuMap.forEach { (key, value) ->
                            kotlin.run {
                                println(key)
                            }
                        }
                    }
                    else -> {

                    }
                }
            }
        }
    }

}